import { _decorator, Component, input, Input, RigidBody2D, Node, Vec2, Collider2D, Contact2DType, Prefab, NodePool, instantiate, Vec3, BoxCollider2D, ERigidBody2DType } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('Game')
export class Game extends Component {


    @property({ type: Node })
    private ballNode: Node = null;

    private ballSpeed: number = 0;
    private blockPool: NodePool = new NodePool(); // 创建一个节点池用于管理木板Prefab


    @property({ type: Prefab })
    private blockPrefab: Prefab = null;
    start() {
        this.collisionHandler();
        input.on(Input.EventType.TOUCH_START, this.onTouchStart, this);
        // this.scheduleBlockGeneration();
        this.initBlock();

    }
    initBlock() {



        const blockCount = 5; // 木板数量
        const blockWidth = 200; // 木板宽度
        const minY = -68.4; // y轴最小位置
        const maxY = -68.4; // y轴最大位置
        const startX = -400; // x轴起始位置
        const endX = 400; // x轴结束位置
        const totalWidth = endX - startX; // x轴总宽度
        let totalGap = totalWidth - blockWidth * blockCount; // 总的间隔距离
        let lastX = startX - blockWidth; // 上一个木板的x坐标，初始化为x的初始位置的左侧

        for (let i = 0; i < blockCount; i++) {
            let newBlock = null;
            if (this.blockPool.size() > 0) {
                newBlock = this.blockPool.get(); // 从节点池中获取一个木板节点
            } else {
                newBlock = instantiate(this.blockPrefab); // 如果节点池中没有可用的节点，则实例化一个新的木板节点
            }



            // 计算间隔距离，确保在50到100之间随机
            let currentGap = 10 + Math.random() * 10;
            lastX += blockWidth / 2 + currentGap; // 计算当前木板的x坐标
            let posY = minY + Math.random() * (maxY - minY); // 计算木板的 y 轴位置

            newBlock.setPosition(new Vec3(lastX, posY, 0)); // 设置木板的位置
            this.node.addChild(newBlock); // 添加木板节点到场景中

            lastX += blockWidth / 2; // 更新lastX到当前木板的右侧


        }


    }



    update(deltaTime: number) {
        this.moveBlocksLeft();
    }

    moveBlocksLeft() {
        const speed = 5; // 移动速度
        this.node.children.forEach((child: Node, index: number) => {
            let posX = child.position.x;
            posX -= speed;
            child.setPosition(new Vec3(posX, child.position.y, 0));
        });
    }

    onTouchStart() {
        if (this.ballSpeed == 0) {
            return;
        }
        let rigidbody = this.ballNode.getComponent(RigidBody2D);
        rigidbody.linearVelocity = new Vec2(0, -30);
    }

    collisionHandler() {
        let collider = this.ballNode.getComponent(Collider2D);
        let rigidbody = this.ballNode.getComponent(RigidBody2D);

        collider.on(Contact2DType.BEGIN_CONTACT, () => {
            if (this.ballSpeed == 0) {
                this.ballSpeed = Math.abs(rigidbody.linearVelocity.y);
            } else {
                rigidbody.linearVelocity = new Vec2(0, this.ballSpeed);
            }
        }, this);
    }

    scheduleBlockGeneration() {
        let interval = 2; // 木板生成间隔时间
        this.schedule(() => {
            this.generateBlock();
        }, interval);
    }

    generateBlock() {
        let newBlock = null;
        if (this.blockPool.size() > 0) {
            newBlock = this.blockPool.get(); // 从节点池中获取一个木板节点
        } else {
            newBlock = instantiate(this.blockPrefab); // 如果节点池中没有可用的节点，则实例化一个新的木板节点
        }
        newBlock.setPosition(this.getRandomBlockPosition());
        this.node.addChild(newBlock);
    }

    getRandomBlockPosition() {
        let randomX = Math.random() * 200 - 100; // x轴随机位置在-100~100之间
        let randomY = -68.4; // y轴位置
        return new Vec3(randomX, randomY, 0);
    }

    // 回收木板节点到节点池
    recycleBlock(blockNode: Node) {
        this.blockPool.put(blockNode);
    }
}


